package com.stud.common.utils;

import com.stud.common.constant.HttpStatus;
import com.stud.common.core.page.TableDataInfo;
import com.stud.common.jdbc.StudPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.stud.common.core.page.PageDomain;
import com.stud.common.core.page.TableSupport;
import com.stud.common.utils.sql.SqlUtil;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 分页工具类
 *
 * @author Stud
 */
public class PageUtils extends PageHelper {

    private static final ThreadLocal<StudPage> MARK_PAGE = new ThreadLocal<>();

    public static void setLocalPage(Page page) {
        LOCAL_PAGE.set(page);
    }

    /**
     * 标记该方法为分页调用,具体到真正分页的语句再调用 startPage();
     */
    public static void markPage() {
        StudPage studPage = new StudPage();
        MARK_PAGE.set(studPage);
    }

    /**
     * 取完即毁
     *
     * @return
     */
    public static boolean isMark() {
        StudPage studPage = MARK_PAGE.get();
        if (studPage == null) {
            return false;
        }
        boolean mark = studPage.isMark();
        MARK_PAGE.remove();
        return mark;
    }

    public static boolean isStart() {
        Page page = LOCAL_PAGE.get();
        return page != null;
    }

    /**
     * 设置请求分页数据
     */
    public static void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
        Boolean reasonable = pageDomain.getReasonable();
        startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
    }

    public static <T> TableDataInfo<T> emptyPage() {
        TableDataInfo<T> rspData = new TableDataInfo<>();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(Lists.newArrayList());
        rspData.setTotal(0);
        rspData.setPageDomain(TableSupport.buildPageRequest());
        return rspData;
    }

    public static <T> TableDataInfo<T> getDataTable(List<T> list, long count) {
        TableDataInfo<T> rspData = new TableDataInfo<>();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(count);
        rspData.setPageDomain(TableSupport.buildPageRequest());
        return rspData;
    }

    public static <T> TableDataInfo<T> getDataTable(List<T> list) {
        if (CollectionUtils.isEmpty(list)) {
            return getDataTable(list, 0);
        }

        return getDataTable(list, new PageInfo<>(list).getTotal());
    }

    /**
     * 清理分页的线程变量
     */
    public static void clearPage() {
        PageHelper.clearPage();
        clearMark();
    }

    /**
     * 清理分页的线程变量
     */
    public static void clearMark() {
        MARK_PAGE.remove();
    }
}
